﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class ManageOrder : BasePage
    {
        private ExcuteDao dao = new ExcuteDao();
        protected void Page_Load(object sender, EventArgs e)
        {
             
            if (Request.QueryString["UrlType"] != null && Request.QueryString["UrlType"].Equals("menu"))
            {
                Session["SortExpression_orderList"] = null;
                Session["SortDirection_orderList"] = null;
                Session["txtname"] = null;
                Session["txtphone"] = null;
                Session["txtmobilphone"] = null;
            }

            if (Session["UrlType"] != null)
            {
                if (Session["UrlType"].Equals("Editorder"))
                {
                    setSessionData();
                    if (Session["orderListIndex"] != null && string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
                    {
                        gvorder.PageIndex = (int)Session["orderListIndex"];
                    }

                    Session["UrlType"] = null;
                }
            }
            if (!string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                string pageIndex = Request.QueryString["pageIndex"];
                int index = Convert.ToInt32(pageIndex);
                gvorder.PageIndex = index;

                Session["orderListIndex"] = index;

                setSessionData();
            }
            //设置分页状态
            if (Session["SortExpression_orderList"] != null)
            {
                this.gvorder.Attributes["SortExpression_orderList"] = Session["SortExpression_orderList"].ToString();
                this.gvorder.Attributes["SortDirection_orderList"] = Session["SortDirection_orderList"].ToString();
                setSortColumnStyle(this.gvorder, Session["SortExpression_orderList"].ToString(), Session["SortDirection_orderList"].ToString());
            }
            //setSortColumnStyle(gvorder, this.gvorder.Attributes["SortExpression_orderList"].ToString(),
            //     this.gvorder.Attributes["SortDirection_orderList"].ToString());
            if (!IsPostBack)
            {
                ViewState["orderAll"] = dao.Get_YUYUE_LIST(
                                                SpecialCharHandler.HtmlEncode(txtname.Text),
                                                SpecialCharHandler.HtmlEncode(txtphone.Text), 
                                                SpecialCharHandler.HtmlEncode(txtmobilphone.Text));

                //初次加载清空分页状态
                //if (string.IsNullOrEmpty(Request.QueryString["pageIndex"]) && Session["UrlType"] == null)
                //{
                //    Session["SortExpression_orderList"] = null;
                //    Session["SortDirection_orderList"] = null;
                //}
                SearchorderBindData();
            }
        }
        private void setSessionData()
        {             
            //检索条件记录
            if (Session["txtname"] != null)
            {
                txtname.Text = SpecialCharHandler.HtmlDiscode(Session["txtname"].ToString());
            }
            if (Session["txtphone"] != null)
            {
                txtphone.Text = SpecialCharHandler.HtmlDiscode(Session["txtphone"].ToString());
            }
            if (Session["txtmobilphone"] != null)
            {
                txtmobilphone.Text = SpecialCharHandler.HtmlDiscode(Session["txtmobilphone"].ToString());
            }
            
            if (Session["orderListIndex"] != null)
            {
                gvorder.PageIndex = (int)Session["orderListIndex"];
            }
        }
        #region 信息列表事件

        /// <summary>
        /// 信息列表数据加载时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvorder_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("id", e.Row.RowIndex.ToString());
                e.Row.Attributes.Add("onKeyDown", "SelectRow();");
                e.Row.Attributes.Add("onClick", "MarkRow(" + e.Row.RowIndex.ToString() + ");");
                ((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "../Customer/OrderDetail.aspx?cID="
                    + SpecialCharHandler.HtmlDiscode(((HyperLink)e.Row.Cells[0].Controls[0]).Text).Replace("&", "amp;");

                e.Row.Cells[1].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[1].Text);

                string jianjie = SpecialCharHandler.HtmlDiscode(e.Row.Cells[6].Text);
                e.Row.Cells[6].ToolTip = jianjie;
                if (jianjie.Length > 15)
                {
                    jianjie = jianjie.Substring(0, 15) + "...";
                }
                e.Row.Cells[6].Text = jianjie;
            }
        }
        #endregion

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState.Clear();
            ViewState["orderAll"] = dao.Get_YUYUE_LIST(
                                                SpecialCharHandler.HtmlEncode(txtname.Text),
                                                SpecialCharHandler.HtmlEncode(txtphone.Text),
                                                SpecialCharHandler.HtmlEncode(txtmobilphone.Text));
            //清空分页状态
            Session["SortExpression_orderList"] = null;
            Session["SortDirection_orderList"] = null;

            //检索条件记录
            Session["txtname"] = SpecialCharHandler.HtmlEncode(txtname.Text);
            Session["txtphone"] = SpecialCharHandler.HtmlEncode(txtphone.Text);
            Session["txtmobilphone"] = SpecialCharHandler.HtmlEncode(txtmobilphone.Text);

            SearchorderBindData();
        }

        /// <summary>
        /// 选择页面功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvorder_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvorder.PageIndex = e.NewPageIndex;
            pageIndex = e.NewPageIndex;
            Session["orderListIndex"] = pageIndex;
            SearchorderBindData();
        }

        /// <summary>
        /// GridView排序事件
        /// </summary>
        protected void gvorder_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string SortExpression_orderList = e.SortExpression.ToString();
            //// 假定为排序方向为“顺序”
            string SortDirection_orderList = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (SortExpression_orderList == this.gvorder.Attributes["SortExpression_orderList"])
            {
                //获得下一次的排序状态
                SortDirection_orderList = (this.gvorder.Attributes["SortDirection_orderList"].ToString() == SortDirection_orderList ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.gvorder.Attributes["SortExpression_orderList"] = SortExpression_orderList;
            this.gvorder.Attributes["SortDirection_orderList"] = SortDirection_orderList;
            //记录分页状态
            Session["SortExpression_orderList"] = SortExpression_orderList;
            Session["SortDirection_orderList"] = SortDirection_orderList;

            setSortColumnStyle(this.gvorder, SortExpression_orderList, SortDirection_orderList);
            this.SearchorderBindData();
        }

        /// <summary>
        /// 绑定到GridView
        /// </summary>
        private void SearchorderBindData()
        {
            // 获取GridView排序数据列及排序方向
            string SortExpression_orderList = this.gvorder.Attributes["SortExpression_orderList"];
            string SortDirection_orderList = this.gvorder.Attributes["SortDirection_orderList"];
            // 调用业务数据获取方法
            DataTable dtBind = (DataTable)ViewState["orderAll"];

            //页面数据总数
            recordCount = dtBind.Rows.Count;

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(SortExpression_orderList)) && (!string.IsNullOrEmpty(SortDirection_orderList)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", SortExpression_orderList, SortDirection_orderList);
            }
            // GridView绑定并显示数据
            this.gvorder.DataSource = dtBind;
            this.gvorder.DataBind();

            pageCount = gvorder.PageCount;
            pageIndex = gvorder.PageIndex;

        }
        protected void gvorder_DataBound(object sender, EventArgs e)
        {
            if (Session["Edit_orderID"] != null)
            {
                foreach (GridViewRow gvr in gvorder.Rows)
                {
                    if (((HyperLink)gvr.Cells[0].Controls[0]).Text.Equals(
                        SpecialCharHandler.HtmlDiscode(Session["Edit_orderID"].ToString())))
                    {
                        //gvorder.SelectedIndex = gvr.RowIndex;
                        //gvorder.Rows[gvr.RowIndex].BackColor = System.Drawing.Color.FromName("#D8EBF0");
                        ClientScript.RegisterStartupScript(GetType(), "SelectRow", "<script>MarkRow(" + gvr.RowIndex + "); </script>");

                    }
                }
                Session["Edit_orderID"] = null;
            }
        }      
    }
}
